package com.guoxin.part4.chapter24;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * ThreadHandler 用于处理每一个提交的Request请求，由于TaskHandler将
 * 被Thread执行，因此需要实现Runnable接口
 *
 * @author guoxin
 * @date 2019/9/2
 */
public class TaskHandler implements Runnable {

    private final Request request;

    public TaskHandler(Request request) {this.request = request;}

    @Override public void run() {
        System.out.println("Begin handle " + request);
        slowly();
        System.out.println("End handle " + request);
    }

    private void slowly() {
        try {
            TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
